package com.company.cloud.user.modules.app.utils;

import java.util.List;
import java.util.Set;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.user.modules.app.models.ThirdUserEntity;
import com.company.cloud.user.modules.app.models.UserInfoCacheModel;
import com.company.cloud.user.modules.cache.utils.UserCacheKeysUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.cache.CacheKeys;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.common.utils.HttpContextUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.user.modules.base.entity.ThirdUserInfo;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.base.entity.UserRoleInfo;

import cn.hutool.core.util.IdUtil;

public class UserUtils {


	/**生成登录用户信息并且保存到缓存*/
	public static LoginUserInfo loginUser2cache(UserInfo userInfo,ThirdUserInfo thirdUser,List<UserRoleInfo> userRoles,
			boolean isSSO,RedisUtils redisUtils, String token, ThirdUserEntity thirdUserEntity) {
		LoginUserInfo user=UserUtils.getLoginUser(userInfo,thirdUser,userRoles,token,thirdUserEntity);
		UserUtils.setUser2cache(user, isSSO, redisUtils);
		return user;
	}

	/**获取登录用户信息*/
	public static LoginUserInfo getLoginUser(UserInfo userInfo,ThirdUserInfo thirdUser,List<UserRoleInfo> userRoles,
											 String token,ThirdUserEntity thirdUserEntity) {
		if (StrUtil.isEmpty(token)) {
			token=CacheKeys.getUserToken();
		}
		LoginUserInfo user=new LoginUserInfo();
		user.setId(-1);
		user.setLangType("ZH");
		user.setIp(HttpContextUtils.getClientIP());
		user.setToken(token);
		user.setKey(IdUtil.simpleUUID());
		UserUtils.initUser(user, userInfo, thirdUser, userRoles,thirdUserEntity);
		return user;
	}
	/**初始化登录用户信息*/
	private static void initUser(LoginUserInfo user,UserInfo userInfo,ThirdUserInfo thirdUser,
								 List<UserRoleInfo> userRoles,ThirdUserEntity thirdUserEntity) {
		if(userInfo!=null) {
			user.setId(userInfo.getId());
			user.setCompanyNo(userInfo.getCompanyNo());
			user.setUserTypeOne(userInfo.getUserTypeOne());
			user.setUserTypeTwo(userInfo.getUserTypeTwo());
			user.setSourceOne(userInfo.getSourceOne());
			user.setSourceTwo(userInfo.getSourceTwo());
			
			user.setUserName(userInfo.getUserName());
			user.setNickName(userInfo.getNickName());
			user.setRealName(userInfo.getRealName());
			user.setHeadImg(userInfo.getHeadImg());
			user.setUserState(userInfo.getUserState());
			user.setNationCode(userInfo.getNationCode());
			user.setMobile(userInfo.getMobile());
			user.setTel(userInfo.getTel());
			user.setEmail(userInfo.getEmail());
			user.setQq(userInfo.getQq());
			user.setReferralCode(userInfo.getReferralCode());
			user.setReferralUserId(userInfo.getReferralUserId());
			user.setShareCode(userInfo.getShareCode());
			user.setIsManager(userInfo.getIsManager());
			user.setDeptId(userInfo.getDeptId());
			
			user.setIsDeptManager(userInfo.getIsDeptManager());
			user.setDataAuthorityType(userInfo.getDataAuthorityType());
			user.setDataAuthorityScope(StrSplitUtils.parse2set(userInfo.getDataAuthorityScope(), String.class));
			user.setJobCode(StrSplitUtils.parse2set(userInfo.getJobCode(), String.class));
			user.setUserTags(StrSplitUtils.parse2set(userInfo.getUserTags(), String.class));
		}


		if(thirdUser!=null) {
			if(userInfo==null){
				user.setId(thirdUser.getUserId());
				user.setCompanyNo(thirdUser.getCompanyNo());
				user.setThirdId(thirdUser.getId());
				user.setThirdUserId(thirdUser.getUserId());
				user.setThirdOpenId(thirdUser.getOpenId());
			}else {
				user.setThirdId(thirdUser.getId());
				user.setThirdUserId(thirdUser.getUserId());

				user.setThirdOpenId(thirdUser.getOpenId());
			}
		}

		if(thirdUserEntity!=null){
			user.setThirdSessionKey(thirdUserEntity.getSessionKey());
		}

		if(!CollectionUtils.isEmpty(userRoles)) {
			for (UserRoleInfo item:userRoles) {
				user.addRoleCode(item.getRoleCode());
			}
		}
	}

	/**登录用户信息保存到缓存*/
	public static void setUser2cache(LoginUserInfo user,boolean isSSO,RedisUtils redisUtils) {
		System.out.println("+++++++++++++++ user 缓存新增");
		System.out.println("key:"+user.getToken());
		System.out.println("缓存前>>> "+ JSON.toJSONString(user));
		redisUtils.set(user.getToken(), user);
		LoginUserInfo u2= redisUtils.get(user.getToken(),LoginUserInfo.class);
		System.out.println("缓存后>>>"+ JSON.toJSONString(u2));

		String userIdCacheKey = CacheKeys.getUserIdCacheKey(user.getId());
		if(isSSO) {
			String userToken = redisUtils.get(userIdCacheKey);
			if(StringUtils.hasText(userToken)) {
				redisUtils.delete(userToken);
			}
			redisUtils.set(userIdCacheKey, user.getToken());
		}else {
			redisUtils.sadd(userIdCacheKey, user.getToken());
		}
	}
	
	public static LoginUserInfo getLoginUserByCache(String token,RedisUtils redisUtils) {
		return redisUtils.get(token, LoginUserInfo.class, 7*24*60*60);
	}
	
	/***/
	public static void logoutUser2cache(LoginSimpleUserInfo userInfo, boolean isSSO, RedisUtils redisUtils) {
		String userIdCacheKey = CacheKeys.getUserIdCacheKey(userInfo.getId());
		if(isSSO) {
			redisUtils.delete(userIdCacheKey);
		}else {
			Set<Object> objTokenCacheKeys= redisUtils.smembers(userIdCacheKey);
			if(!CollectionUtils.isEmpty(objTokenCacheKeys)) {
				for(Object k : objTokenCacheKeys){
					if (k.toString().equals(userInfo.getToken())) {
						objTokenCacheKeys.remove(k);
						redisUtils.sremove(userIdCacheKey, k.toString());
					}
				}
			}
			redisUtils.delete(userIdCacheKey);
		}
		redisUtils.delete(userInfo.getToken());
	}
	
	

}
